package de.koo.grum.controllers

import grails.converters.*
import de.koo.grum.domain.*

class ContactDataController {
    
	def index = { redirect(action:list,params:params) }
	
    // the delete, save and update actions only accept POST requests
    def static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        //if(!params.max) params.max = 50
        def criteria = ContactData.createCriteria()
        def contactDataList
        def searchString=""
        
        if(params.searchString) {
            searchString=params.searchString

            contactDataList = criteria.list {
                or { ilike("data","%${searchString}%")
        			ilike("type","%${searchString}%")
        			user { 
                        or { ilike("firstname","%${searchString}%")
	        				ilike("lastname","%${searchString}%")
                        }
        			}
                }
        	}
        }
        else {
        	contactDataList=ContactData.list( params )
        }
        
        // Content Negotiation, serves the format, that requested
        // test with curl
        withFormat {
            xml {
            	// renders default XML
            	//render contactDataList as XML
            	
            	// render my own XML struktur
            	render(contentType:"text/xml"){
            	    contacts{
            	    	for(c in contactDataList){
            	    		contact{
            	    			firstname(c.user.firstname)
            	    			lastname(c.user.lastname)
            	    			type(c.type)
            	    			data(c.data)
            	    		}
            	    	}        
            	    }
            	}
            }
            html {
            	[ contactDataList: contactDataList, searchString:searchString
            	  , resultSize:contactDataList.size(), pageSize:50]
            }
        }
        [ contactDataList: contactDataList, searchString:searchString
          , resultSize:contactDataList.size(), pageSize:50]
    }

    def confirmDelete = {
            flash.message = "Do you want to delete ContactData ${params.id}?"
            [ contactData : ContactData.get( params.id ) ]
    }

    def edit = {
        def contactData = ContactData.get( params.id )

        if(!contactData) {
            flash.message = "ContactData not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ contactData : contactData ]
        }
    }

    def update = {
        def contactData = ContactData.get( params.id )
        if(contactData) {
            contactData.properties = params
            if(!contactData.hasErrors() && contactData.save(flush:true)) {
                flash.message = "ContactData ${params.id} updated"
                redirect(controller:'user',action:edit,id:contactData.user.id)
            }
            else {
                render(view:'edit',model:[contactData:contactData])
            }
        }
        else {
            flash.message = "ContactData not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
            def contactData = new ContactData()
            def user = new User();
            
            contactData.properties = params
            
            if (params.user_id) {
            	user=User.get( params.user_id )
            }
            
            return ['contactData':contactData, 'user': user]
        }

    def save = {
        def contactData = new ContactData()
        contactData.properties = params
        contactData.user=User.get(params.User.id)
        
        if(contactData.save(flush:true)) {
            flash.message = "ContactData ${contactData.id} created."
            redirect(controller:'user',action:edit,id:contactData.user.id)
        }
        else {
            render(view:'create',model:[contactData:contactData])
        }
    }
}